var ns;
var app = new Vue({
    el: '#app',
    data: {
        form: {
            keywords: '',
            exceptids: ''
        },
        list: [],
        showchoose: false,
        showsearch: false,//遮罩层
        chooseList: [],
        ID: 0,//工单ID
        totalQuantity: 0,
    },
    methods: {
        showMoreOpen: function (item) {
            var that = this;
            item.isopen = !item.isopen;
        },
        doHideSearch: function () {
            var that = this;
            that.showchoose = false;
            that.showsearch = false;
        },
        showChooseAll: function () {
            var that = this;
            that.showchoose = !that.showchoose;
            that.showsearch = !that.showsearch;//底部阴影

        },
        // input值改变触发
        onChangeQuantity: function (item) {
            var that = this;
            if (item.backNumber != '') {
                item.backNumber = Number(item.backNumber);
            }

            if (item.backNumber <= 0 || item.backNumber == '') {
                item.backNumber = 0;
                const index = that.chooseList.findIndex(p => p.ID === item.ID);
                that.chooseList.splice(index, 1)
            }
            // 返还总数=返还数+返还中的数量+已返还数量
            let pruductNumber = item.backNumber + item.ApplicationBackCount + item.ApproveBackCount;
            if (item.ApproveOutCount < pruductNumber) {
                api.toast({
                    msg: '返还数量不能大于实际领用数量',
                    duration: 2000,
                    location: 'bottom'
                });
                item.backNumber = (pruductNumber - item.ApplicationBackCount - item.ApproveBackCount);
                that.addTotalQuantity();//input 值改变事件重新计算
                return;
            } 
            that.addTotalQuantity();//input 值改变事件重新计算
        },
        addTotalQuantity: function () {
            var that = this;
            that.totalQuantity = 0;
            if (that.chooseList.length > 0) {
                for (var i = 0; i < that.chooseList.length; i++) {
                    that.totalQuantity += Number(that.chooseList[i].backNumber);
                }
            }
        },
        delQuantity: function (item) {
            var that = this;
            item.ischecked = false;
            if (item.backNumber>0) {
                item.backNumber--;
            }
            if (item.backNumber==0) {
                const index = that.chooseList.findIndex(p => p.ID === item.ID);
                that.chooseList.splice(index, 1)
            }
            that.totalQuantity--;
        },
        addQuantity: function (item) {
            var that = this;
            // 返还总数=返还数+返还中的数量+已返还数量
            let pruductNumber = item.backNumber + item.ApplicationBackCount + item.ApproveBackCount;
            if (item.ApproveOutCount <= pruductNumber) {
                api.toast({
                    msg: '返还数量不能大于实际领用数量',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            };
            item.ischecked = true;
            if (item.backNumber == '' || item.backNumber == 0) {
                item.backNumber = 1;
            } else {
                item.backNumber++;
            }
            that.chooseList.push(item);
            const new_list = [...new Set(that.chooseList)];// 去重
            that.chooseList = new_list;
            that.totalQuantity++;
        },
     
        get_data: function() {
            var that = this;
            that.list = [];
            var options = {
                action: 'getserviceproductlist',
                ServiceID: that.ID,
                KeyWords: that.form.keywords,
            };
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.list = data.list;
                } else if (err) {
                    that.list = [];
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        do_search: function() {
            var that = this;
            that.get_data();
        },
        do_choose: function(item) {
            var that = this;
            item.ischecked = !item.ischecked;
        },
        do_save: function() {
            var that = this;
            var list = [];
            //for (var i = 0; i < that.list.length; i++) {
            //    var item = that.list[i];
            //    for (var j = 0; j < item.children.length; j++) {
            //        var backNumber = Number(item.children[j].backNumber);
            //        if (item.children[j].ischecked && backNumber > 0) {
            //            list.push(item.children[j]);
            //        }
            //    }
            //}
            list = that.chooseList;
            if (list.length == 0) {
                api.toast({
                    msg: '请选择商品',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            //点击确认返还,系统后台生成对应的物料返还申请单
            var options = {};
            options.ID = that.ID;
            options.productList = JSON.stringify(list);
            options.action = "customerservicebackproduct";
            ns.post(options, function (succeed, data, err) {
                if (data.status) {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                    //刷新详情界面
                    api.sendEvent({
                        name: 'updata_task_con',
                    });
                    setTimeout(function () {
                        api.closeWin();
                    }, 1000);
                } else if (!data.status) {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
/*                toastmsg: '提交中'*/
            });
            
        },
        // 物品领料删除
        productDelete:function(ApplicationID){
            let that = this;
            api.confirm({
                title: '提示',
                msg: '确认删除当前领料信息',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        let options = {
                            action:"productapplicationdelete",
                            ApplicationID:ApplicationID
                        };
                        ns.post(options, function(succeed, data, err) {
                            if(data.status){
                                that.get_data();
                                //刷新详情界面
                                api.sendEvent({
                                    name: 'updata_task_con',
                                });
                            }
                            else{
                                if(data.Msg){
                                    api.toast({
                                        msg: data.Msg,
                                        duration: 2000,
                                        location: 'bottom'
                                    });
                                    return;
                                };
                                api.toast({
                                    msg: "请求失败",
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            };
                        }, {
                            toast: true
                        });
                    };
                };
            });
        },
        // 物品领料关闭
        productClose:function(ApplicationID){
            let that = this;
            api.confirm({
                title: '提示',
                msg: '确认关闭当前领料信息',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret) {
                    if (ret.buttonIndex == 1) {
                        let options = {
                            action:"productapplicationclose",
                            ApplicationID:ApplicationID
                        };
                        ns.post(options, function(succeed, data, err) {
                            if(data.status){
                                that.get_data();
                                //刷新详情界面
                                api.sendEvent({
                                    name: 'updata_task_con',
                                });
                            }
                            else{
                                if(data.Msg){
                                    api.toast({
                                        msg: data.Msg,
                                        duration: 2000,
                                        location: 'bottom'
                                    });
                                    return;
                                };
                                api.toast({
                                    msg: "请求失败",
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            };
                        }, {
                            toast: true
                        });
                    };
                };
            });
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    ns.init_search();
    app.form.exceptids = api.pageParam.exceptids || '';
    app.ID = api.pageParam.ID || 0;
    app.get_data();
}
